package org.example.mybooksystem.Controller;

import lombok.extern.slf4j.Slf4j;
import org.example.mybooksystem.Modle.*;
import org.example.mybooksystem.Service.BookService;
import org.example.mybooksystem.constant.Constants;
import org.example.mybooksystem.enums.ResultCode;
import org.example.mybooksystem.mapper.BookInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:做渐进主义的改良派,不做完没注意的速成派
 * User: XALEXC
 * Date: 2024-12-07
 * Time: 6:05
 */
@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    BookService bookService;
    @Autowired
    private BookInfoMapper bookInfoMapper;

  /*  @RequestMapping("/getBookListByPage")
    private PageResult<BookInfo> getBookListByPage(PageRequest pageRequest) {
        log.info("查询翻页信息,pageRequest:{}", pageRequest);
        if (pageRequest.getPageSize() < 0 || pageRequest.getCurrentPage() < 1) {
            return null;
        }
        PageResult<BookInfo> bookInfoPageResult = null;
        try {
            bookInfoPageResult = bookService.selectBookInfoByPage(pageRequest);
        } catch (Exception e) {
            log.error("查询翻页信息错误,e:{}", e);
        }

        return bookInfoPageResult;
    }*/

    @RequestMapping("/getBookListByPage")
    private Result getBookListByPage(PageRequest pageRequest, HttpSession session) {
        log.info("查询翻页信息,pageRequest:{}", pageRequest);
        /*// 校验用户是否登录,      去查看session的内容
        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
        if(userInfo==null||userInfo.getId()<=0||"".equals(userInfo.getUserName())) {
            // 用户未登录
            System.out.println("1");
          return Result.unlogin();
        }*/
        // 成功登录后的信息
        if (pageRequest.getPageSize() < 0 || pageRequest.getCurrentPage() < 1) {
            return Result.fail("参数校验失败");
        }
        PageResult<BookInfo> bookInfoPageResult = null;
        try {
            bookInfoPageResult = bookService.selectBookInfoByPage(pageRequest);
            return Result.success(bookInfoPageResult);
        } catch (Exception e) {
            log.error("查询翻页信息错误,e:{}", e);
          return Result.fail(e.getMessage());
        }

    }

    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBook(BookInfo bookInfo) {
        log.info("接收到添加图书请求,bookInfo:{}",bookInfo);
        //在这里调用service
        if(!StringUtils.hasLength(bookInfo.getBookName())
        || !StringUtils.hasLength(bookInfo.getAuthor())
        || bookInfo.getCount()<0
        ||bookInfo.getPrice()==null
        ||!StringUtils.hasLength(bookInfo.getPublish())){
            return "参数校验失败";
        }

        Integer result=bookInfoMapper.insertBookInfo(bookInfo);
        if(result<=0){
            log.error("添加图书出错:bookInfo:{}",bookInfo);
            return "添加图书出错,请联系管理员";
        }
        return "";
    }

    @RequestMapping("/queryBookInfoById")
    public BookInfo queryBookInfoById(Integer bookId){
        log.info("根据ID查询图书,bookId:{}",bookId);
        try{
            BookInfo bookInfo=bookService.queryBookInfoById(bookId);
            return bookInfo;
        }catch (Exception e){
            log.error("查询图书失败,e:{}",e.getMessage());
        }
        return null;
    }

    @RequestMapping(value = "/updateBook",produces = "application/json")
    public String updateBookInfo(BookInfo bookInfo){
        log.info("接收到更新图书的请求,bookInfo:{}",bookInfo);
        Integer result=bookService.updateBook(bookInfo);
        if(result==0){
            log.error("更新图书失败,请联系管理员");
            return "更新图书失败,请联系管理员";
        }
        return "";
    }

    @RequestMapping(value = "/batchDelete",produces = "application/json")
    public String batchDelete(@RequestParam List<Integer> ids){
        log.info("接受请求,批量删除图书,图书ID:{}",ids);
        Integer result = bookService.batchDelete(ids);
        if(result<=0){
            log.error("批量删除失败,请联系管理员");
            return "批量删除失败,请联系管理员";
        }
        return "";
    }
}
